include("shared.lua")
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

--[[

	// ATTENTION: THIS MAY LOOK A LOT LIKE THE WARMELON CODING, BUT I CAN ASSURE YOU. IT IS 100% MY OWN CODE
	IT WAS ORIGINALLY INTENDED TO BE A REWRITE OF THE WARMELONS (And it was. Just never going to release)
	IT WORKS WITH THE CURRENT THIS STYLE, SO I HAVE NO PLANS TO CHANCE IT.
	- TechedRonan

--]]

ENT.UseGurgle 			= true				// Change me if you find the hounds anoying. (The gurgling sounds)


ENT.Model 				= "models/props_junk/watermelon01.mdl"
ENT.Material 			= ""

ENT.Speed				= 420				// Speed, if using physics then this might need to be put quite a lot higher.

ENT.HP					= 50				// HP.. it's HP.

ENT.MoveDistance		= 2000				// The distance(in units) that it will detect enemies within, and attempt moving closer.
ENT.AttackDistance		= 300				// The distance that it will attack enemies within.

ENT.AttackDelay			= 3					// Delay in seconds between attacks
ENT.AttackDelayR		= 0.8				// Random delay to add or substract

function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( "melonhound" )
		ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	
	return ent
	
end

function ENT:Initialize()
	util.PrecacheSound("NPC_FastZombie.Gurgle")
	util.PrecacheSound("NPC_FastZombie.LeapAttack")
	util.PrecacheSound("Zombie.Pain")
	self.Entity:SetModel(self.Model);
	self.Entity:SetMaterial(self.Material);
	self.Entity:PhysicsInit(SOLID_VPHYSICS);
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS);
	self.Entity:SetSolid(SOLID_VPHYSICS);
	
	local physics = self.Entity:GetPhysicsObject();
	if (physics:IsValid()) then
		physics:Wake()
	end
	
	self.Dead = false
	
	self.Target = nil
	self.Attacking = false
	self.Moving = false
	
	self.Move = true
	
	self.NextAttack = CurTime()
end

function ENT:Think ()	
	self.Target = nil
	self.Attacking = false
	self.Moving = false
	// Check if there are any enemies within move radius.
	local enemies = ents.FindInSphere( self.Entity:GetPos(), self.MoveDistance )
		local CEnemy = self:GetClosest( enemies )
		for k, v in pairs(enemies) do
			if( CEnemy and CEnemy:GetPos():Distance( self.Entity:GetPos() ) < self.AttackDistance ) then
				self.Attacking = true
				self.Target = CEnemy
				break
			else
				self.Moving = true
				self.Target = CEnemy
			break
		end
	end
	
	if( self.Target and self.Moving and !self.Dead ) then
		local angle = self.Target:GetPos() - self.Entity:GetPos();
		self.Entity:GetPhysicsObject():ApplyForceCenter(angle:Normalize() * self.Speed)
		if( self.UseGurgle ) then
			self.Entity:EmitSound("NPC_FastZombie.Gurgle")
		end
	else
		self.Entity:StopSound("NPC_FastZombie.Gurgle")
	end
	
	if( self.Target and self.Attacking and !self.Dead ) then
		if( CurTime() > self.NextAttack ) then
			self:FireWeapon()
			self.Entity:EmitSound("NPC_FastZombie.LeapAttack")
			self.NextAttack = CurTime() + self.AttackDelay + math.random( -self.AttackDelayR, self.AttackDelayR )
		end
	end
	
	
	if( self.Dead and CurTime() > self.FadeTimer ) then
		self.Alpha = self.Alpha - 55
		self.Entity:SetColor(0, 0, 0, self.Alpha)
		if( self.Alpha - 55 <= 0 ) then
			self.Entity:Remove()
		else
			self.Alpha = self.Alpha - 55
		end
	end
end

function ENT:FireWeapon()
	// Default shit, wont be used if overridden
	local traceRes=util.QuickTrace (self.Entity:GetPos(), self.Target:GetPos()-self.Entity:GetPos(), self.Entity)
	local traceDown=util.QuickTrace (self.Entity:GetPos(), self.Entity:GetPos(), self.Entity)
	--if( traceDown.HitWorld ) then
		local angle = self.Target:GetPos() + Vector( 0, 0, 50 ) - self.Entity:GetPos();
		self.Entity:GetPhysicsObject():SetVelocity(angle:Normalize()*900);
	--end
end

function ENT:OnTakeDamage (dmginfo)
	// Implement the armor later.
	local dmg = dmginfo:GetDamage()
	// Will it survive the blow?
	if( self.HP - dmg > 0 ) then
		self.HP = self.HP - dmg
		self.Entity:EmitSound("Zombie.Pain")
	else
		local effectdata = EffectData()
		effectdata:SetOrigin( self.Entity:GetPos() )
		util.Effect( "BloodImpact", effectdata )
		
		local PMelon = ents.Create( "prop_physics" )
		PMelon:SetModel("models/props_junk/watermelon01.mdl")
		PMelon:SetAngles(self.Entity:GetAngles())
		PMelon:SetPos(self.Entity:GetPos())
		PMelon:SetOwner(self.Entity)
		PMelon:Spawn()
		PMelon:Activate()
		PMelon:Fire("break", "", 0)
		self.Entity:StopSound("NPC_FastZombie.Gurgle")
		self.Entity:Remove()
	end
end

function ENT:GetClosest( Table )
	local Closest = 9000000
	local Ent = nil
	local Pos = self.Entity:GetPos()
	
	if( Table ) then
		for k, v in pairs(Table) do
			if( v:GetPos():Distance( Pos ) < self.MoveDistance ) then
				if( v:GetPos():Distance( Pos ) < Closest ) then
					if( v:IsPlayer() and v:Alive() or v:IsNPC() ) then
						Closest = v:GetPos():Distance( Pos )
						Ent = v
					end
				end
			end
		end
	else
		return nil
	end
	
	if( Ent ) then
		return Ent
	else
		return nil
	end
end

function ENT:OnRemove()
	self.Entity:StopSound("NPC_FastZombie.Gurgle")
end
